package com.ruoyi.project.work.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.project.work.domain.WorkProcessItem;
import com.ruoyi.project.work.mapper.WorkProcessMapper;
import com.ruoyi.project.work.domain.WorkProcess;
import com.ruoyi.project.work.service.IWorkProcessService;

/**
 * 工作流程Service业务层处理
 * 
 * @author wyh
 * @date 2024-08-12
 */
@Service
public class WorkProcessServiceImpl implements IWorkProcessService 
{
    @Autowired
    private WorkProcessMapper workProcessMapper;

    /**
     * 查询工作流程
     * 
     * @param id 工作流程主键
     * @return 工作流程
     */
    @Override
    public WorkProcess selectWorkProcessById(Long id)
    {
        return workProcessMapper.selectWorkProcessById(id);
    }

    /**
     * 查询工作流程列表
     * 
     * @param workProcess 工作流程
     * @return 工作流程
     */
    @Override
    public List<WorkProcess> selectWorkProcessList(WorkProcess workProcess)
    {
        return workProcessMapper.selectWorkProcessList(workProcess);
    }

    /**
     * 新增工作流程
     * 
     * @param workProcess 工作流程
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkProcess(WorkProcess workProcess)
    {
        int rows = workProcessMapper.insertWorkProcess(workProcess);
        insertWorkProcessItem(workProcess);
        return rows;
    }

    /**
     * 修改工作流程
     * 
     * @param workProcess 工作流程
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkProcess(WorkProcess workProcess)
    {
        workProcessMapper.deleteWorkProcessItemByWorkId(workProcess.getId());
        insertWorkProcessItem(workProcess);
        return workProcessMapper.updateWorkProcess(workProcess);
    }

    /**
     * 批量删除工作流程
     * 
     * @param ids 需要删除的工作流程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkProcessByIds(Long[] ids)
    {
        workProcessMapper.deleteWorkProcessItemByWorkIds(ids);
        return workProcessMapper.deleteWorkProcessByIds(ids);
    }

    /**
     * 删除工作流程信息
     * 
     * @param id 工作流程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkProcessById(Long id)
    {
        workProcessMapper.deleteWorkProcessItemByWorkId(id);
        return workProcessMapper.deleteWorkProcessById(id);
    }

    /**
     * 新增工作流程条目信息
     * 
     * @param workProcess 工作流程对象
     */
    public void insertWorkProcessItem(WorkProcess workProcess)
    {
        List<WorkProcessItem> workProcessItemList = workProcess.getWorkProcessItemList();
        Long id = workProcess.getId();
        if (StringUtils.isNotNull(workProcessItemList))
        {
            List<WorkProcessItem> list = new ArrayList<WorkProcessItem>();
            for (WorkProcessItem workProcessItem : workProcessItemList)
            {
                workProcessItem.setWorkId(id);
                list.add(workProcessItem);
            }
            if (list.size() > 0)
            {
                workProcessMapper.batchWorkProcessItem(list);
            }
        }
    }
}
